package com.example.demo.nuoqi.controller;

import com.example.demo.nuoqi.pojo.Direction;
import com.example.demo.nuoqi.pojo.Itemdetails;
import com.example.demo.nuoqi.pojo.User;
import com.example.demo.nuoqi.service.DirectionService;
import com.example.demo.nuoqi.service.ItemdetailsService;
import com.example.demo.nuoqi.utils.JsonUtils;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import com.example.demo.nuoqi.utils.UserUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "ItemdetailsController")
@RestController
@RequestMapping("/itemdetails")
@CrossOrigin
public class ItemdetailsController {

    @Autowired
    private ItemdetailsService itemdetailsService;
    @Autowired
    private DirectionService directionService;

    /***
     * Itemdetails分页条件搜索实现
     * @param itemdetails
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Itemdetails条件分页查询",notes = "分页条件查询Itemdetails方法详情",tags = {"ItemdetailsController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Itemdetails对象",value = "传入JSON数据",required = false) Itemdetails itemdetails, @PathVariable int page, @PathVariable int size){
        //调用ItemdetailsService实现分页条件查询Itemdetails
        PageInfo<Itemdetails> pageInfo = itemdetailsService.findPage(itemdetails, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Itemdetails分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Itemdetails分页查询",notes = "分页查询Itemdetails方法详情",tags = {"ItemdetailsController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size){
        //调用ItemdetailsService实现分页查询Itemdetails
        PageInfo<Itemdetails> pageInfo = itemdetailsService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     *
     * @param itemdetails
     * @return
     */
    @ApiOperation(value = "Itemdetails条件查询",notes = "条件查询Itemdetails方法详情",tags = {"ItemdetailsController"})
    @PostMapping(value = "/search" )
    public Result<List<Itemdetails>> findList(@RequestBody(required = false) @ApiParam(name = "Itemdetails对象",value = "传入JSON数据",required = false) Itemdetails itemdetails){
        //调用ItemdetailsService实现条件查询Itemdetails
        List<Itemdetails> list = itemdetailsService.findList(itemdetails);
        return new Result<List<Itemdetails>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Itemdetails根据ID删除",notes = "根据ID删除Itemdetails方法详情",tags = {"ItemdetailsController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用ItemdetailsService实现根据主键删除
        itemdetailsService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Itemdetails数据
     * @param itemdetails
     * @param id
     * @return
     */
    @ApiOperation(value = "Itemdetails根据ID修改",notes = "根据ID修改Itemdetails方法详情",tags = {"ItemdetailsController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Itemdetails对象",value = "传入JSON数据",required = false) Itemdetails itemdetails, @PathVariable Integer id){
        //设置主键值
        itemdetails.setId(id);
        //调用ItemdetailsService实现修改Itemdetails
        itemdetailsService.update(itemdetails);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Itemdetails数据
     * @param itemdetails
     * @return
     */
    @ApiOperation(value = "Itemdetails添加",notes = "添加Itemdetails方法详情",tags = {"ItemdetailsController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Itemdetails对象",value = "传入JSON数据",required = true) Itemdetails itemdetails , HttpSession session){
        //调用ItemdetailsService实现添加Itemdetails

        User user = (User) session.getAttribute("user");
        itemdetails.setUserId(user.getId());
        itemdetails.setCreateTime(new Date());

        itemdetails.setItemStatus("1");
        itemdetails.setCreatePerson(user.getName());
        itemdetailsService.add(itemdetails);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Itemdetails数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Itemdetails根据ID查询",notes = "根据ID查询Itemdetails方法详情",tags = {"ItemdetailsController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Itemdetails> findById(@PathVariable Integer id){
        //调用ItemdetailsService实现根据主键查询Itemdetails
        Itemdetails itemdetails = itemdetailsService.findById(id);

        List<Itemdetails.participantTemp> temps = JsonUtils.jsonToList(itemdetails.getParticipant(), Itemdetails.participantTemp.class);
        itemdetails.setParticipantTemp(temps);

        return new Result<Itemdetails>(true,StatusCode.OK,"查询成功",itemdetails);
    }

    /***
     * 查询Itemdetails全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Itemdetails",notes = "查询所Itemdetails有方法详情",tags = {"ItemdetailsController"})
    @GetMapping
    public Result<List<Itemdetails>> findAll(){
        //调用ItemdetailsService实现查询所有Itemdetails
        List<Itemdetails> list = itemdetailsService.findAll();
        List<Itemdetails> temp = new ArrayList<>();
        for (Itemdetails itemdetails : list) {
            Direction byId = directionService.findById(Integer.valueOf(itemdetails.getTechnicalDirection()));
            itemdetails.setDirecTionName(byId.getDirectionName());
            temp.add(itemdetails);
        }
        return new Result<List<Itemdetails>>(true, StatusCode.OK,"查询成功",temp) ;
    }


    /***
     * 查询当前人创建的项目
     * @return
     */
    @ApiOperation(value = "查询当前人创建的项目",notes = "查询当前人创建的项目",tags = {"ItemdetailsController"})
    @GetMapping("/findByUserId")
    public Result<List<Itemdetails>> findByUserId(HttpSession session){
        //调用ItemdetailsService实现查询所有Itemdetails
        User user = UserUtils.getUser(session);
        List<Itemdetails> list = itemdetailsService.findAll();
        List<Itemdetails> temp = new ArrayList<>();

        for (Itemdetails itemdetails : list) {
            if (itemdetails.getUserId() == user.getId()) {
                temp.add(itemdetails);
            }
        }
        return new Result<List<Itemdetails>>(true, StatusCode.OK,"查询成功",temp) ;
    }


    /***
     * 创建人结束项目/终止项目
     * @return
     */
    @ApiOperation(value = "创建人结束项目",notes = "创建人结束项目",tags = {"ItemdetailsController"})
    @GetMapping("/terminaItem/{itemId}/{result}")
    public Result terminaItem(@PathVariable("itemId")Integer itemId,@PathVariable("result")String result){
        //调用ItemdetailsService实现查询所有Itemdetails
        Itemdetails id = itemdetailsService.findById(itemId);
        id.setItemStatus(result);
        itemdetailsService.update(id);
        return new Result(true, StatusCode.OK,"项目以结束",null) ;
    }



    /***
     * 设置项目状态
     * @return
     */
    @ApiOperation(value = "设置项目状态",notes = "设置项目状态",tags = {"ItemdetailsController"})
    @GetMapping("/setItemResult/{itemId}/{result}")
    public Result setItemResult(@PathVariable("itemId")Integer itemId,@PathVariable("result")String result){
        //调用ItemdetailsService实现查询所有Itemdetails
        Itemdetails id = itemdetailsService.findById(itemId);
        id.setItemStatus(result);
        itemdetailsService.update(id);
        return new Result(true, StatusCode.OK,"项目状态修改完成",null) ;
    }





}
